<!--#
layout("/layouts/platform.html"){
#-->
<style>
    .el-upload input[type=file] {
        display: none;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 128px;
        height: 128px;
        line-height: 128px;
        text-align: center;
    }

    .avatar {
        width: 128px;
        height: 128px;
        display: block;
    }
</style>
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">

        <div class="pull-right offscreen-right mt5">
            <el-button size="medium" @click="openAdd"><i class="ti-plus"></i>新增地址</el-button>
        </div>
    </el-row>
    <el-row class="el-table-container">
        <el-table
                :data="tableData"
                size="small"
                header-align="center"
                style="width: 100%"
        >
            <el-table-column
                    prop="address"
                    label="地址"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
            </el-table-column>
            <!--备注-->
            <el-table-column
                    sortable
                    prop="remark"
                    label="备注"
                    header-align="center"
                    align="center">
            </el-table-column>
            <el-table-column
                    sortable
                    prop="disabled"
                    label="状态"
                    header-align="center"
                    align="center">
                <template slot-scope="scope">
                    <i v-if="scope.row.disabled" class="fa fa-circle text-danger ml5"></i>
                    <i v-if="!scope.row.disabled" class="fa fa-circle text-success ml5"></i>
                </template>
            </el-table-column>
            <el-table-column
                    sortable
                    label="创建时间"
                    header-align="center"
                    align="center">
                <template slot-scope="scope">
                    {{formatAt(scope.row.ctAt)}}
                </template>
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="120"
                    align="center">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                                    :command="{type:'edit',id:scope.row.id,name:scope.row.name,row:scope.row}">
                                修改
                            </el-dropdown-item>
                            <el-dropdown-item divided
                                              :command="{type:'enable',id:scope.row.id,name:scope.row.name,row:scope.row}">
                                启用
                            </el-dropdown-item>
                            <el-dropdown-item
                                    :command="{type:'disable',id:scope.row.id,name:scope.row.name,row:scope.row}">
                                禁用
                            </el-dropdown-item>
                            <el-dropdown-item :command="{type:'delete',id:scope.row.id,row:scope.row}">
                                删除
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <!--分页容器-->
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>
    <!--新增/修改按钮弹出的dialog-->
    <el-dialog
            title="地址属性"
            :visible.sync="editDialogVisible"
            :close-on-click-modal="false"
            width="40%">
        <el-form :model="formData" ref="editForm" :rules="formRules" size="small" label-width="100px">

            <el-form-item prop="address" label="地址">
                <el-input maxlength="100" placeholder="地址"
                          v-model="formData.address"
                          auto-complete="off"
                          tabindex="1"
                          type="text"
                >
                </el-input>
            </el-form-item>
            </el-form-item>
            <el-form-item prop="remark" label="备注">
                <el-input maxlength="100" placeholder="备注"
                          v-model="formData.remark"
                          auto-complete="off"
                          tabindex="1"
                          type="text"
                >
                </el-input>
            </el-form-item>
            <el-form-item prop="disabled" label="启用状态">
                <el-switch
                        v-model="formData.disabled"
                        active-color="#ff4949"
                        inactive-color="#13ce66">
                </el-switch>
            </el-form-item>

        </el-form>
        <!--        确认/取消 提交修改按钮-->
        <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doEdit">确 定</el-button>
        </span>
    </el-dialog>
</div>
<script>
    new Vue({
        el: "#app",
        data: function () {
            var self = this;
            return {
                pickerOptions: {
                    disabledDate: function (time) {
                        return time.getTime() > new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000);
                    },
                    shortcuts: [{
                        text: '最近一周',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'weeks').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-3, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                searchDate: [],
                editDialogVisible: false,
                titlePopu: 0,
                tableData: [],
                pageForm: {
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    searchKeyword: "",
                    searchDate: ""
                },
                formData: {
                    no: "",
                    name: "",
                    cardNo: "",
                    bankName: "",
                    subBranch: "",
                    disabled: false
                },
                formRules: {
                    address: [
                        {required: true, message: '请输入地址', trigger: 'blur'}
                    ],
                },
                loading: false,
                options: []
            }
        },
        methods: {
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                var self = this;
                sublime.showLoadingbar();//显示loading
                if (self.searchDate) {
                    self.pageForm.searchDate = self.searchDate.toString();
                } else {
                    self.pageForm.searchDate = "";
                }
                $.post(base + "/platform/sys/ercAddress/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code == 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            openAdd: function () {// 新建/修改User按钮逻辑
                var self = this;
                self.titlePopu = 0;
                this.editDialogVisible = true;
                //打开新增窗口,表单先清空
                this.formData = {
                    anchor: false
                };
                if (this.$refs["editForm"]) {
                    this.$refs["editForm"].resetFields();
                }
            },
            doEdit: function () { // 提交新增/修改
                var self = this;
                self.$refs["editForm"].validate(function (valid) {
                    if (valid) {
                        //titlePopu=0是新增,1是修改
                        $.post(base + "/platform/sys/ercAddress/" + ( self.titlePopu == 0 ? "add" : "editDo"), self.formData, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.doSearch();
                                self.editDialogVisible = false;
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            dropdownCommand: function (commond) {
                var self = this;
                switch (commond.type) {
                    case "disable" : {
                        this.setStatus(commond);
                        return;
                    }
                    case "enable" : {
                        this.setStatus(commond);
                        return;
                    }
                    case "delete" : {
                        this.deleteAddr(commond.id)
                        return;
                    }
                    case "edit" : {
                        $.post(base + "/platform/sys/ercAddress/edit/" + commond.id, function (data) {
                            if (data.code == 0) {
                                self.openAdd();
                                self.formData = data.data;//加载后台表单数据
                                self.editDialogVisible = true;//打开编辑窗口
                                self.titlePopu = 1;
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        })
                        return;
                    }
                }
            },
            setStatus: function (obj) {
                var self = this;
                $.post(base + "/platform/sys/ercAddress/" + obj.type, {id: obj.id}, function (data) {
                    if (data.code == 0) {
                        self.$message({
                            message: data.msg,
                            type: 'success'
                        });
                        self.doSearch();
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            deleteAddr: function (id) { // 提交新增/修改
                var self = this;
                self.$confirm('确认删除此地址', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    callback: function (a, b) {
                        if ("confirm" == a) {//确认后再执行
                            $.post(base + "/platform/sys/ercAddress/del", {id: id}, function (data) {
                                if (data.code == 0) {
                                    self.$message({
                                        message: data.msg,
                                        type: 'success'
                                    });
                                    self.doSearch();
                                    self.editDialogVisible = false;
                                } else {
                                    self.$message({
                                        message: data.msg,
                                        type: 'error'
                                    });
                                }
                            }, "json");
                        }
                    }
                })
            },
            formatAt: function (val) {
                if (val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm");
                return "";
            },
            handlePicSuccess: function (response, file, fileList, attrName) {// 上传图片
                if (response.code == 0) {
                    // 对象属性直接赋值不会触发试图更新，采用 Vue.set 、this.$set或者Object.assign({}，this.obj)创建新对象
                    // 或者也可以定义一个 refresh 属性 ，当数据发生改变时，该属性同时变化，也会触发视图更新
                    // this.formData.site_logo =response.data;
                    this.$set(this.formData, attrName, response.data);
                } else {
                    this.$set(this.formData, attrName, "");
                }
            },
        },
        created: function () {
            this.pageData();
        }
    });
</script>

<!--#
}
#-->